Partial Class Admin_Add_Broker
  Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Country_cmb.Items.Count < 1 Then
      Dim zClass As New LookUp.General_Lookup
      Country_cmb.Items.Clear()
      zClass.Loader(Country_cmb, "COUNTRY", "country_desc", "country_cd")
      Country_cmb.Items.Insert(0, "")
      zClass.LookUpCode = "BROKER_CONTACT"

      If Session("broker_mode") = "View" Then
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, Session("broker_cd"))
        LoadDetails()
        DisableMost()
      ElseIf Session("broker_mode") = "Edit" Then
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, Session("broker_cd"))
        LoadDetails()
      End If

    End If
    SecurePage()
  End Sub

  Protected Sub Country_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Country_cmb.SelectedIndexChanged
    If Country_cmb.SelectedItem.Text = "" Then Exit Sub

    Dim zClass As New LookUp.General_Lookup
    City_cmb.Items.Clear()
    zClass.Loader(City_cmb, "CITY", "city_desc", "city_cd", Country_cmb.SelectedValue)
    City_cmb.Items.Insert(0, "")

  End Sub

  Private Sub DisableMost()
    DataGrid.Columns(5).Visible = False : DataGrid.Columns(6).Visible = False
    Save_btn.Enabled = False : Website_txt.Enabled = False
    Name_txt.Enabled = False : Add1_txt.Enabled = False : Add2_txt.Enabled = False
    City_cmb.Enabled = False : Country_cmb.Enabled = False : Zip_txt.Enabled = False
    T1_txt.Enabled = False : M1_txt.Enabled = False : F1_txt.Enabled = False
    Start_txt.Enabled = False : End_txt.Enabled = False : Add_btn.Enabled = False
  End Sub

  Private Sub LoadDetails()
        Dim sConn As New Data.SqlClient.SqlConnection
        Dim sCom As New Data.SqlClient.SqlCommand
        Dim sDApt As New Data.SqlClient.SqlDataAdapter(sCom)
        Dim sDT As New Data.DataTable
        Dim zClass As New LookUp.General_Lookup
        Dim cf As New CIMSfunc

        Try
            sConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("CnnStr")
            sCom.CommandType = Data.CommandType.Text
            sCom.Connection = sConn
            sCom.CommandText = "SELECT * FROM broker_mstr WHERE broker_cd='" & Session("broker_cd") & "'"
            sConn.Open()
            sDApt.Fill(sDT)

            With sDT.Rows(0)

                Code_txt.Text = .Item(0).ToString : Name_txt.Text = .Item(1).ToString
                Add1_txt.Text = .Item(2).ToString : Add2_txt.Text = .Item(3).ToString

                Country_cmb.SelectedItem.Selected = False
                If Trim(.Item(5).ToString) <> "" Then Country_cmb.Items.FindByValue(.Item(5).ToString).Selected = True

                City_cmb.Items.Clear()
                zClass.Loader(City_cmb, "CITY", "city_desc", "city_cd", Country_cmb.SelectedValue)
                City_cmb.Items.Insert(0, "")
                If Trim(.Item(4).ToString) <> "" Then City_cmb.Items.FindByValue(.Item(4).ToString).Selected = True

                Zip_txt.Text = Trim(.Item(6).ToString) : T1_txt.Text = Trim(.Item(7).ToString)
                M1_txt.Text = Trim(.Item(9).ToString) : F1_txt.Text = Trim(.Item(8).ToString)
                Start_txt.Text = cf.ParseGetDate(.Item(11).ToString) 'Month(.Item(11).ToString) & "/" & Day(.Item(11).ToString) & "/" & _
                'Year(.Item(11).ToString)
                End_txt.Text = cf.ParseGetDate(.Item(12).ToString) 'Month(.Item(12).ToString) & "/" & Day(.Item(12).ToString) & "/" & _
                'Year(.Item(11).ToString)
                Website_txt.Text = .Item(10).ToString

            End With

        Catch ex As Exception

        Finally
            sConn.Close()
        End Try

        cf = Nothing
  End Sub

  Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
    Session.Remove("broker_mode") : Session.Remove("broker_cd")
    Response.Redirect("~/Admin/Brokers.aspx")
  End Sub

  Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO
        Dim cf As New CIMSfunc
        Dim strTemp As String = ""

    If dm.Q(Trim(cf.ParseSetDate(End_txt.Text))) <= dm.Q(Trim(cf.ParseSetDate(Start_txt.Text))) Then
      Dim pf As New PolyFunc.ASPFunc
      pf.MessageAlert(Me, "End Date should be greater than Start Date!")
      pf = Nothing
      End_txt.Focus()
      Exit Sub

    End If

        If Session("broker_mode") = "Edit" Then
      zClass.ExecQuery("UPDATE broker_mstr SET broker_desc=" & dm.Q(Trim(Name_txt.Text)) & _
          ", broker_address=" & UCase(dm.Q(Trim(Add1_txt.Text))) & _
          ", broker_address2=" & UCase(dm.Q(Trim(Add2_txt.Text))) & _
          ", city_cd=" & UCase(dm.Q(Trim(City_cmb.SelectedValue))) & _
          ", country_cd=" & UCase(dm.Q(Trim(Country_cmb.SelectedValue))) & _
          ", zip_cd=" & UCase(dm.Q(Trim(Zip_txt.Text))) & _
          ", tel_no=" & UCase(dm.Q(Trim(T1_txt.Text))) & _
          ", fax_no=" & UCase(dm.Q(Trim(F1_txt.Text))) & _
          ", mobile_no=" & UCase(dm.Q(Trim(M1_txt.Text))) & _
          ", broker_website=" & UCase(dm.Q(Trim(Website_txt.Text))) & _
          ", broker_start_dt=" & dm.Q(Trim(cf.ParseSetDate(Start_txt.Text))) & _
          ", broker_end_dt=" & dm.Q(Trim(cf.ParseSetDate(End_txt.Text))) & _
          ", broker_last_upd_by='" & Session("UserName").ToString & "', broker_last_upd_dt='" & _
          Now & "'" & _
          " WHERE broker_cd='" & Trim(Session("broker_cd").ToString) & "'")

        ElseIf Session("broker_mode") = "Add" Then
            'must have city, country, start_date, and name

            zClass.LookUpCode = "BROKER"
            strTemp = zClass.MaxCode()
            zClass.ExecQuery("INSERT INTO broker_mstr(broker_cd, broker_desc, broker_address, " & _
            "broker_address2, city_cd, country_cd, zip_cd, tel_no, fax_no, mobile_no, " & _
            "broker_website, broker_start_dt, " & _
            IIf(Trim(End_txt.Text) <> "", "broker_end_dt, ", "") & _
            "broker_crea_by, broker_crea_dt) " & _
            "VALUES('" & strTemp & "'" & _
                    "," & UCase(dm.Q(Trim(Name_txt.Text))) & _
                    "," & UCase(dm.Q(Trim(Add1_txt.Text))) & _
                    "," & UCase(dm.Q(Trim(Add2_txt.Text))) & _
                    "," & UCase(dm.Q(Trim(City_cmb.SelectedValue))) & _
                    "," & UCase(dm.Q(Trim(Country_cmb.SelectedValue))) & _
                    "," & UCase(dm.Q(Trim(Zip_txt.Text))) & _
                    "," & UCase(dm.Q(Trim(T1_txt.Text))) & _
                    "," & UCase(dm.Q(Trim(F1_txt.Text))) & _
                    "," & UCase(dm.Q(Trim(M1_txt.Text))) & _
                    "," & UCase(dm.Q(Trim(Website_txt.Text))) & _
                    "," & dm.Q(Trim(cf.ParseSetDate(Start_txt.Text))) & _
                    "," & dm.Q(Trim(cf.ParseSetDate(End_txt.Text))) & _
                    ",'" & Session("UserName").ToString & "','" & Month(Now) & "/" & Day(Now) & "/" & Year(Now) & "')")
            Session("broker_cd") = strTemp

        End If

        Session("broker_mode") = "Edit"
        zClass.LookUpCode = "BROKER_CONTACT"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, Session("broker_cd"))
        LoadDetails()

        cf = Nothing
    'Response.Redirect("~/Admin/Brokers.aspx")
  End Sub

  Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
    If e.CommandName = "Edit" Or e.CommandName = "View" Then
      Session("contact_cd") = e.Item.Cells(0).Text
      Session("contact_mode") = e.CommandName
      Response.Redirect("~/Admin/Add_Contact_Broker.aspx")

    ElseIf e.CommandName = "Delete" Then
      Dim zClass As New LookUp.General_Lookup

      'If zClass.OKtoDELETE(e.Item.Cells(0).Text) = True Then Exit Sub

      zClass.ExecQuery("DELETE FROM broker_contact_dtl WHERE broker_contact_cd='" & _
        e.Item.Cells(0).Text & "' AND broker_cd='" & Session("broker_cd") & "'")

      zClass.LookUpCode = "BROKER_CONTACT"
      DataGrid.CurrentPageIndex = 0
      zClass.DataGridFill(DataGrid, Results_lbl, Session("broker_cd"))
      LoadDetails()
    End If
  End Sub

  Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
    Session("contact_mode") = "Add"
    Response.Redirect("~/Admin/Add_Contact_Broker.aspx")

  End Sub

  Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
    DataGrid.CurrentPageIndex = e.NewPageIndex
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpCode = "BROKER_CONTACT"
    zClass.DataGridFill(DataGrid, Results_lbl, Session("broker_cd"))
    LoadDetails()
  End Sub
End Class